import threading
import json
import tkinter as tk
import time as tm


with open('01-1.json', 'r') as f:
	ft = f.read()
	around = json.loads(ft)
	print(around)

## 初始化 ##
top = tk.Tk()
top.geometry('300x300+100+100')
fr1 = tk.Frame(top)
fr2 = tk.Frame(top)
fr3 = tk.Frame(top)
fr1.pack()
fr2.pack()
fr3.pack()

## 设置标签、按钮 ##
l1 = tk.Label(fr1, bg='#000000', width=10, height=5)
l2 = tk.Label(fr1, bg='#ffffff', width=10, height=5)
l3 = tk.Label(fr1, bg='#ffffff', width=10, height=5)
l4 = tk.Label(fr2, bg='#ffffff', width=10, height=5)
l5 = tk.Label(fr2, bg='#000000', width=10, height=5)
l6 = tk.Label(fr2, bg='#ffffff', width=10, height=5)
l7 = tk.Label(fr3, bg='#ffffff', width=10, height=5)
l8 = tk.Label(fr3, bg='#ffffff', width=10, height=5)
l9 = tk.Label(fr3, bg='#000000', width=10, height=5)
for i in [l1, l2, l3, l4, l5, l6, l7, l8, l9]:
	i.pack(side=tk.LEFT)


oldlst = [None,
	[l1, 1],
	[l2, 0],
	[l3, 0],
	[l4, 0],
	[l5, 1],
	[l6, 0],
	[l7, 0],
	[l8, 0],
	[l9, 1]
]
newlst = oldlst[:]

## 侦测函数 ##
def LookPos(pos):
	global oldlst, newlst
	global around
	if pos > 9:
		return -1
	life = 0
	arounds = around[str(pos)]
	for i in arounds:
		if oldlst[int(i)][1] == 1:
			life += 1
	print(life)
	if life == 2 or life ==3:
		newlst[pos][0].config(bg='#000000')
		newlst[pos][1] = 1
	else:
		newlst[pos][0].config(bg='#ffffff')
		newlst[pos][1] = 0
	oldlst = newlst[:]
	return 0

## 单次侦测函数 ##
def OneLook():
	for i in range(1, 10):
		LookPos(i)
b1 = tk.Button(top, text='done', command=OneLook)
b1.pack()
tk.mainloop()

'''
附录1 —— 试验标签位置与大小
tk.Label(fr1, bg='#ffffff', width=10, height=5).pack(side=tk.LEFT)
tk.Label(fr1, bg='#000000', width=10, height=5).pack(side=tk.LEFT)
tk.Label(fr1, bg='#ffffff', width=10, height=5).pack(side=tk.LEFT)
tk.Label(fr2, bg='#000000', width=10, height=5).pack(side=tk.LEFT)
tk.Label(fr2, bg='#ffffff', width=10, height=5).pack(side=tk.LEFT)
tk.Label(fr2, bg='#000000', width=10, height=5).pack(side=tk.LEFT)
tk.Label(fr3, bg='#ffffff', width=10, height=5).pack(side=tk.LEFT)
tk.Label(fr3, bg='#000000', width=10, height=5).pack(side=tk.LEFT)
tk.Label(fr3, bg='#ffffff', width=10, height=5).pack(side=tk.LEFT)
'''

